import os, shutil, subprocess


def main():
    nowPath = os.getcwd()
    name    = "自动打卡"
    time = input("添加一个打卡时间, 格式 HH:MM ,24小时制: ")
    for k, i in enumerate(time):
        if i == "：":
            time = time[:k] + ":" + time[k+1:]

    xml=\
    '''<?xml version="1.0" encoding="UTF-16"?>
    <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
    <RegistrationInfo>
        <Date>2020-08-03T12:09:45.1502204</Date>
        <Author>厦门大学</Author>
        <URI>''' + f"{name}_{time.split(':')[0] + time.split(':')[1]}" + '''</URI>
    </RegistrationInfo>
    <Triggers>
        <CalendarTrigger>
        <StartBoundary>2020-08-03T'''+ time +''':00</StartBoundary>
        <Enabled>true</Enabled>
        <ScheduleByDay>
            <DaysInterval>1</DaysInterval>
        </ScheduleByDay>
        </CalendarTrigger>
    </Triggers>
    <Principals>
        <Principal id="Author">
        <LogonType>InteractiveToken</LogonType>
        <RunLevel>HighestAvailable</RunLevel>
        </Principal>
    </Principals>
    <Settings>
        <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
        <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
        <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
        <AllowHardTerminate>true</AllowHardTerminate>
        <StartWhenAvailable>false</StartWhenAvailable>
        <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
        <IdleSettings>
        <StopOnIdleEnd>true</StopOnIdleEnd>
        <RestartOnIdle>false</RestartOnIdle>
        </IdleSettings>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <Enabled>true</Enabled>
        <Hidden>false</Hidden>
        <RunOnlyIfIdle>false</RunOnlyIfIdle>
        <WakeToRun>false</WakeToRun>
        <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
        <Priority>7</Priority>
    </Settings>
    <Actions Context="Author">
        <Exec>
        <Command>''' + f"{nowPath}\\main.exe" + '''</Command>
        <WorkingDirectory>'''+ f"{nowPath}" + '''</WorkingDirectory>
        </Exec>
    </Actions>
    </Task>'''
    with open("config.xml", "w") as f:
        f.write(xml)

    os.system(f"SCHTASKS /Create /TN {name}_{time.split(':')[0] + time.split(':')[1]} /XML config.xml")
    os.system("pause")

if __name__ == "__main__":
    main()